#include "FieldDialog.h"
#include <QHBoxLayout>

FieldDialog::FieldDialog(Format *format, FERange range, void *context)
{
    Qt::WindowFlags flags = Qt::Dialog;
    flags |= Qt::WindowMinMaxButtonsHint;
    flags |= Qt::WindowCloseButtonHint;
    setWindowFlags(flags);

    view_ = new FieldView();

    model_ = new FieldModel(format, range, context, this);
    connect(view_, &FieldView::nodePressed, model_, &FieldModel::selectItem);

    view_->setModel(model_);
    tab = new BufferTab();
    tab->selectedChanged(range);
    connect(model_, &FieldModel::rangeChanged, tab, &BufferTab::selectedChanged);

    splitter = new QSplitter(Qt::Horizontal);
    splitter->addWidget(view_);
    splitter->addWidget(tab);
    splitter->setOpaqueResize(false);
//    splitter->setStretchFactor(0, 1);
//    splitter->setStretchFactor(1, 3);
//    splitter->setSizes(QList<int>() << 1 << 1);
    auto layout = new QHBoxLayout(this);
    layout->addWidget(splitter);
    setLayout(layout);
}

FieldDialog::~FieldDialog()
{
    view_->setModel(nullptr);
    delete view_;
    delete model_;
}

void FieldDialog::closeEvent(QCloseEvent *event) {
    QDialog::closeEvent(event);
    emit closed(this);
}
